package com.rongc.client.freight.business.waybill.view.activity;

import android.view.View;
import android.widget.EditText;
import android.widget.RatingBar;

import com.android.volley.Response;
import com.android.volley.error.VolleyError;
import com.rongc.client.core.exception.RegexException;
import com.rongc.client.core.network.code.HandleCode;
import com.rongc.client.core.network.http.RequestManager;
import com.rongc.client.core.utils.ActivityUtils;
import com.rongc.client.core.utils.LogUtils;
import com.rongc.client.core.utils.RegexUtil;
import com.rongc.client.freight.R;
import com.rongc.client.freight.base.BaseActivity;
import com.rongc.client.freight.base.request.result.NullResult;
import com.rongc.client.freight.base.view.activity.HostActivity;
import com.rongc.client.freight.business.waybill.request.api.WaybillReplyApi;
import com.rongc.client.freight.utils.UtilMethod;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by ccqx on 2017/8/19.
 */

public class ReplyActivity extends BaseActivity implements View.OnClickListener{
    @Bind(R.id.et_content)
    EditText mEtContent;
    @Bind(R.id.rating)
    RatingBar mRating;

    @OnClick({R.id.btn_submit})
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_submit:
                try{
                    prepare();
                    RequestManager.getInstance().call(new WaybillReplyApi(
                            new WaybillReplyApi.WaybillReplyParams(waybillid,
                                    mEtContent.getText().toString(),mRating.getRating()+""),
                            respOrderListener, errorListener));
                }catch (RegexException e){
                    ActivityUtils.toast(e.getMessage());
                }
                break;
        }
    }

    Response.Listener<NullResult> respOrderListener = new Response.Listener<NullResult>() {

        @Override
        public void onResponse(NullResult response) {
            if (HandleCode.requestSuccess()) {
                ActivityUtils.startActivity(ReplyActivity.this, HostActivity.class);
            } else {
                UtilMethod.dismissProgressDialog(ReplyActivity.this);
            }
        }
    };

    Response.ErrorListener errorListener = new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            LogUtils.i("error.getMessage(): " + error.getMessage());
            UtilMethod.dismissProgressDialog(ReplyActivity.this);
        }
    };


    void prepare() throws RegexException {
        if(!RegexUtil.checkNotNull(mEtContent.getText().toString())){
            throw new RegexException("请输入反馈内容");
        }

    }
    String waybillid ;

    @Override
    public void initParams() {
        waybillid = getIntent().getStringExtra("waybillid");
    }

    @Override
    public void initView() {
        initToolbar("评价");

    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_reply;
    }
}
